﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DotNetNuke.Entities.Users;

public partial class DesktopModules_DNN_Tickets_DNN_Tickets_view : DotNetNuke.Entities.Modules.PortalModuleBase// : System.Web.UI.UserControl
{
    private DNN_Tickets.DNN_TicketsDBDataContext context;
    private UserInfo _currentUser = DotNetNuke.Entities.Users.UserController.GetCurrentUserInfo();
    Guid _currentTicketID;
    protected void Page_Load(object sender, EventArgs e)
    {
        context = new DNN_Tickets.DNN_TicketsDBDataContext();

        var myTickets = from c in context.DNNTicket_Tickets
                        where c.Ticket_From == _currentUser.UserID
                        && c.Ticket_Status <= 3
                        select c;



        GridView1.DataSource = myTickets;
        GridView1.DataBind();


        if (Page.IsPostBack != true)
        {
            Session["_currentTicketID"] = null;

          

            Panel1.Visible = false;
            txtNewMessage.Visible = false;
            td1.Visible = false;
            tr1.Visible = false;
            btnSubmit.Visible = false;
            lblConvHeader.Visible = false;
            tr3.Visible = false;

        }
        else
        {
            Panel1.Visible = true;
            txtNewMessage.Visible = true;
            td1.Visible = true;
            tr1.Visible = true;
            btnSubmit.Visible = true;
            lblConvHeader.Visible = true;
            tr3.Visible = true;
        }


        if (Session["_currentTicketID"] == null)
        {
          
            //if (string.IsNullOrEmpty(Session["_currentTicketID"].ToString()) == true || Session["_currentTicketID"].ToString() == "")
            //{
               
            //Panel1.Visible = false;
            //txtNewMessage.Visible = false;
            //td1.Visible = false;
            //tr1.Visible = false;
            //btnSubmit.Visible = false;

            
        }
        else
        {
            //Panel1.Visible = true;
            //txtNewMessage.Visible = true;
            //td1.Visible = true;
            //tr1.Visible = true;
            //btnSubmit.Visible = true;
        }

    }

    

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        //Response.Write(e.Row.Cells[0].Text);
        e.Row.Cells[0].Visible = false;
    }

    

    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            context = new DNN_Tickets.DNN_TicketsDBDataContext();
           
            GridView dgrid = (GridView)sender;
         
            GridViewRow row = dgrid.SelectedRow;// GridView1.SelectedRow;

            _currentTicketID = new Guid(row.Cells[0].Text);
            Session["_currentTicketID"] = _currentTicketID;


            var myConversations = from cc in context.DNNTicket_Conversations
                                  orderby cc.DateSent ascending
                                  where cc.Ticket_ID == _currentTicketID
                                  select cc;
            
            Repeater1.DataSource = myConversations;
            Repeater1.DataBind();



            lblConvHeader.Text = "Conversation Details for Ticket: " + row.Cells[1].Text;


        }
        catch (Exception ex)
        {
            Response.Write(ex.ToString());
            Response.Write(ex.StackTrace.ToString());
            Response.Write(ex.Message.ToString());
        }

    }
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        //Response.Write("yoooooooooooooo" + e.ToString());
        //GridView dgrid = (GridView)sender;
        //GridViewRow row = dgrid.SelectedRow;
    }


    public string UserFullName(string UserID)
    {
        int id = int.Parse(UserID);
        UserInfo _DNNUser = DotNetNuke.Entities.Users.UserController.GetUserById(0, id);

        return _DNNUser.FirstName + " " + _DNNUser.LastName;
    }
    //public string UserFullName(int UserID)
    //{
    //    UserInfo _DNNUser = DotNetNuke.Entities.Users.UserController.GetUserById(0, UserID);

    //    return _DNNUser.LastName + ", " + _DNNUser.FirstName;
    //}
   
    protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        Label lbl1 = (Label)e.Item.FindControl("lblTicketFromID2");

        if (lbl1 != null)
        {

            Label lbl2 = (Label)e.Item.FindControl("lblTicketFrom2");
            if (lbl2 != null)
            {

                lbl2.Text = UserFullName(lbl1.Text);
                lbl1.Visible = false;
            }
        }

        
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        if (String.IsNullOrEmpty(txtNewMessage.Text.Trim()) != true)
        {
            context = new DNN_Tickets.DNN_TicketsDBDataContext();
            DNN_Tickets.DNNTicket_Conversation cvr = new DNN_Tickets.DNNTicket_Conversation();

            _currentTicketID = new Guid(Session["_currentTicketID"].ToString());


            context.DNNTicket_Conversations.InsertOnSubmit(new DNN_Tickets.DNNTicket_Conversation() { Conversation_Details = txtNewMessage.Text.Trim(), Ticket_ID = _currentTicketID, Conversation_ID = Guid.NewGuid(), Ticket_To = _currentUser.UserID, DateSent = DateTime.Now });

            //DNN_Tickets.DNNTicket_Conversation conver = new DNN_Tickets.DNNTicket_Conversation();
            //conver.Conversation_Details = txtNewMessage.Text;
            txtNewMessage.Text = "";

            context.SubmitChanges();
            
            
            var myConversations = from cc in context.DNNTicket_Conversations
                                  orderby cc.DateSent ascending
                                  where cc.Ticket_ID == _currentTicketID
                                  select cc;

            Repeater1.DataSource = myConversations;
            Repeater1.DataBind();

            var myTickets = from c in context.DNNTicket_Tickets
                            where c.Ticket_From == _currentUser.UserID
                            && c.Ticket_Status <= 3
                            select c;

                 GridView1.DataSource = myTickets;
                 GridView1.DataBind();
        }
        

        //        txtNewMessage.Text
    }

    protected void btnSubmitClose_Click(object sender, EventArgs e)
    {
        if (String.IsNullOrEmpty(txtNewMessage.Text.Trim()) != true)
        {
            context = new DNN_Tickets.DNN_TicketsDBDataContext();
            DNN_Tickets.DNNTicket_Conversation cvr = new DNN_Tickets.DNNTicket_Conversation();

            _currentTicketID = new Guid(Session["_currentTicketID"].ToString());


            context.DNNTicket_Conversations.InsertOnSubmit(new DNN_Tickets.DNNTicket_Conversation() { Conversation_Details = txtNewMessage.Text.Trim(), Ticket_ID = _currentTicketID, Conversation_ID = Guid.NewGuid(), Ticket_To = _currentUser.UserID, DateSent = DateTime.Now });

            //DNN_Tickets.DNNTicket_Conversation conver = new DNN_Tickets.DNNTicket_Conversation();
            //conver.Conversation_Details = txtNewMessage.Text;
            txtNewMessage.Text = "";

            //context.SubmitChanges();


            //var myConversations = from cc in context.DNNTicket_Conversations
            //                      orderby cc.DateSent ascending
            //                      where cc.Ticket_ID == _currentTicketID
            //                      select cc;

            //Repeater1.DataSource = myConversations;
            //Repeater1.DataBind();

            var singleTicket = context.DNNTicket_Tickets.Single(c => c.Ticket_ID == _currentTicketID);
            singleTicket.Ticket_Status = 4;

            context.SubmitChanges();

            Session["_currentTicketID"] = null;


            var myTickets = from c in context.DNNTicket_Tickets
                            where c.Ticket_From == _currentUser.UserID && c.Ticket_Status <= 3
                            select c;


            GridView1.DataSource = myTickets;
            GridView1.DataBind();

            Repeater1.DataSource = null;
            Repeater1.DataBind();

            Panel1.Visible = false;
            txtNewMessage.Visible = false;
            td1.Visible = false;
            tr1.Visible = false;
            btnSubmit.Visible = false;
            lblConvHeader.Visible = false;
            tr3.Visible = false;

        }

    }

    protected void btnTicketSubmit_Click(object sender, EventArgs e)
    {
        DNN_Tickets.DNN_TicketsDBDataContext ncontext = new DNN_Tickets.DNN_TicketsDBDataContext();
        string _title = txtTicket1.Text.Trim();
        Guid _Ticket_ID = Guid.NewGuid();
        DNN_Tickets.DNNTicket_Ticket ticket = new DNN_Tickets.DNNTicket_Ticket() 
        {
            Ticket_From = _currentUser.UserID, 
            Ticket_To = 0,
            Ticket_Title = _title, 
            Ticket_Status = 0, 
            Ticket_ID = _Ticket_ID 
        };

        Session["_currentTicketID"] = _Ticket_ID;

        ncontext.DNNTicket_Tickets.InsertOnSubmit(ticket);
              
        ncontext.SubmitChanges();

        txtTicket1.Text = "";
        lblConvHeader.Text = "Conversation Details for Ticket: " + _title;


    }




    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {

        try
        {
            //DNN_Tickets.DNN_TicketsDBDataContext rcontext = new DNN_Tickets.DNN_TicketsDBDataContext();

            //GridView dgrid = (GridView)sender;

            //GridViewRow row = dgrid.SelectedRow;// GridView1.SelectedRow;
            
            //_currentTicketID = new Guid(row.Cells[0].Text);
            ////_currentTicketID = Guid.NewGuid();

            //Response.Write("" + "<<<123123asdssssssssssssssssssss>>>>" + _currentTicketID.ToString());

            //Session["_currentTicketID"] = null;

            ////var myConversations = from cc in context.DNNTicket_Conversations
            ////                      orderby cc.DateSent ascending
            ////                      where cc.Ticket_ID == _currentTicketID
            ////                      select cc;

            ////Repeater1.DataSource = myConversations;
            ////Repeater1.DataBind();

            //var singleTicket = rcontext.DNNTicket_Tickets.Single(c => c.Ticket_ID == _currentTicketID);
            //singleTicket.Ticket_Status = 4;
           
            //context.SubmitChanges();

        }
        catch (Exception ex)
        {
            Response.Write(ex.ToString());
            Response.Write(ex.StackTrace.ToString());
            Response.Write(ex.Message.ToString());
        }
    }
}